import { Controller } from 'egg';

class ProjectController extends Controller {
    /**
     * 创建项目
     */
    public async createProject(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_name: 'string',
                project_picture: 'string',
                project_content: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.service.projectService.createProject(request.body);
        ctx.body = this.ctx.helper.responseSuccess('创建成功');
    }

    /**
     * 删除项目
     */
    public async deleteProject(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.service.projectService.deleteProject(request.body);
        ctx.body = this.ctx.helper.responseSuccess('删除成功');
    }

    /**
     * 修改项目
     */
    public async updateProject(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id: 'string',
                project_name: 'string',
                project_picture: 'string',
                project_content: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.service.projectService.updateProject(request.body);
        ctx.body = this.ctx.helper.responseSuccess('修改成功');
    }

    /**
     * 查找项目
     */
    public async searchProject(){
        const { ctx, ctx: { request } } = this;
        const result:any=await this.service.projectService.searchProject(request.body);
        ctx.body = this.ctx.helper.responseSuccess('查找成功',result);
    }

    /**
     * 根据Id查找项目详情
     */
    public async searchProjectById(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result:any=await this.service.projectService.searchProjectById(request.body);
        ctx.body = this.ctx.helper.responseSuccess('查找成功',result);
    }
}
export default ProjectController;